package com.java1234.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;

import com.java1234.model.Manager;
import com.java1234.model.User;
import com.java1234.model.animal;


public class UserDao {

	public User login(Connection con,User user)throws Exception{
		User resultUser=null;
		Statement stmt = null;
		String sql="select * from db_book.dbo.t_user where userName='"+user.getUserName()+"' and password='"+user.getPassword()+"'";
		stmt=con.createStatement();
		ResultSet rs=stmt.executeQuery(sql);
		if(rs.next()) {
			resultUser=new User();
			resultUser.setUserName(rs.getString("userName"));
			resultUser.setPassword(rs.getString("password"));
		}
		return resultUser;
	}
	public int update(Connection con,User user)throws Exception{
		String sql="update db_book.dbo.t_user set password=?,Name=?,sex=? ,tele=? where userName=?";
		PreparedStatement pstmt = con.prepareStatement(sql);
		pstmt.setString(1, user.getPassword());
		pstmt.setString(2, user.getName());
		pstmt.setString(3, user.getSex());
		pstmt.setString(4, user.getTele());
		pstmt.setString(5, user.getUserName());


		return pstmt.executeUpdate();
	}
	public User reset(Connection con,String user,String tele)throws Exception{
		User resultUser=null;
		Statement stmt = null;
		String sql="select * from db_book.dbo.t_user where userName='"+user+"' and tele='"+tele+"'";
		stmt=con.createStatement();
		ResultSet rs=stmt.executeQuery(sql);
		if(rs.next()) {
			resultUser=new User();
			resultUser.setUserName(rs.getString("userName"));
			resultUser.setPassword(rs.getString("tele"));
		}
		return resultUser;
	}
	public int resetinter(Connection con,String user,String password)throws Exception{
		String sql="update db_book.dbo.t_user set password=? where userName=?";
		PreparedStatement pstmt = con.prepareStatement(sql);
		pstmt.setString(1, password);
		pstmt.setString(2, user);
		return pstmt.executeUpdate();
	}
}
